package org.shangda.wechat.pet.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

/**
 * 微信开放平台api请求工具类
 */
@Slf4j
public class WeixinOpenUtil {

    /**
     * 获取第三方平台令牌url
     */
    private static String COMPONENT_ACCESSTOKEN_URL = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
    /**
     * 创建预授权码url
     */
    private static String COMPONENT_CREATE_PREAUTHCODE_URL = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=%s";

    /**
     * 使用授权码获取授权信息URL
     */
    private static String COMPONENT_QUERY_AUTH_URL = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=%s";

    /**
     * 第三方平台授权页面
     */
    public static String AUTHORIZE_PAGE_URL = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s";

    /**
     * 已授权的微信公众号的令牌刷新接口
     */
    private static String WECHAT_AUTHORIZER_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=%s";

    /**
     * 发送客服消息接口
     */
    private static String WECHAT_CUSTOM_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s";

    /**
     * 获取第三方平台令牌
     * @param componentVerifyTicket String
     * @param componentAppsecret String
     * @param componentAppid String
     * @return JSONObject
     */
    public static JSONObject getComponentAccessToken(String componentVerifyTicket, String componentAppsecret, String componentAppid){
        log.info("getComponentAccessToken componentVerifyTicket : {}, componentAppid : {}, componentAppsecret : {}", componentVerifyTicket, componentAppid, componentAppsecret);
        JSONObject params = new JSONObject();
        params.put("component_verify_ticket", componentVerifyTicket);
        params.put("component_appsecret", componentAppsecret);
        params.put("component_appid", componentAppid);
        String result = HttpUtil.doPostJson(COMPONENT_ACCESSTOKEN_URL, params);
        log.info("getComponentAccessToken result: {}", result);
        return JSONObject.parseObject(result);
    }

    /**
     * 获取预授权码
     * @param componentAccessToken String
     * @param componentAppid String
     * @return JSONObject
     */
    public static JSONObject getComponentPreauthcode(String componentAccessToken, String componentAppid){
        log.info("getComponentPreauthcode  componentAccessToken : {}, componentAppid : {}", componentAccessToken, componentAppid);
        JSONObject params = new JSONObject();
        params.put("component_appid", componentAppid);
        String result = HttpUtil.doPostJson(String.format(COMPONENT_CREATE_PREAUTHCODE_URL, componentAccessToken), params);
        log.info("getComponentPreauthcode result: {}", result);
        return JSONObject.parseObject(result);
    }

    /**
     * 使用授权码获取授权信息
     * @param componentAccessToken String
     * @param componentAppid String
     * @param authorizationCode String
     * @return JSONObject
     */
    public static JSONObject queryAuthInfo(String componentAccessToken, String componentAppid, String authorizationCode){
        log.info("queryAuthInfo componentAccessToken : {}, componentAppid : {}, authorizationCode : {}", componentAccessToken, componentAppid, authorizationCode);
        JSONObject params = new JSONObject();
        params.put("component_appid", componentAppid);
        params.put("authorization_code", authorizationCode);
        String result = HttpUtil.doPostJson(String.format(COMPONENT_QUERY_AUTH_URL, componentAccessToken), params);
        log.info("queryAuthInfo result: {}", result);
        return JSONObject.parseObject(result);
    }

    /**
     * 获取已授权的微信公众号的令牌
     * @param componentAccessToken String
     * @param componentAppid String
     * @return JSONObject
     */
    public static JSONObject getAuthorizerAccessToken(String componentAccessToken, String componentAppid, String authorizerRefreshToken, String authorizerAppid){
        log.info("getAuthorizerAccessToken  componentAccessToken : {}, componentAppid : {}, authorizerRefreshToken : {}, authorizerAppid : {}", componentAccessToken, componentAppid, authorizerRefreshToken, authorizerAppid);
        JSONObject params = new JSONObject();
        params.put("component_appid", componentAppid);
        params.put("authorizer_appid", authorizerAppid);
        params.put("authorizer_refresh_token", authorizerRefreshToken);
        String result = HttpUtil.doPostJson(String.format(WECHAT_AUTHORIZER_TOKEN_URL, componentAccessToken), params);
        log.info("getAuthorizerAccessToken result: {}", result);
        return JSONObject.parseObject(result);
    }

    /**
     * 发送客服消息
     * @param componentAccessToken
     * @param toUser
     * @param content
     * @return
     */
    public static JSONObject sendTextMsg(String componentAccessToken, String toUser, String content){
        JSONObject params = new JSONObject();
        params.put("touser", toUser);
        params.put("msgtype", "text");

        JSONObject text = new JSONObject();
        text.put("content",content);
        params.put("text", text);
        String result = HttpUtil.doPostJson(String.format(WECHAT_CUSTOM_SEND_URL, componentAccessToken), params);
        log.info("sendTextMsg result: {}", result);
        return JSONObject.parseObject(result);
    }

    public static void main(String[] args) {
        String componentVerifyTicket = "ticket@@@Qnj-FPAmmLVnVOkNOjggxOCwfQ1PW19TqegucQuhJc8MTwP_D4SON6dK4-BVAXPWvHtqUi2asjdCR1xMxG4ZMg";
        String componentAppsecret = "416cd885e23a154c6a3b649c1470393f";
        String componentAppid = "wxcf314a2d45d072f9";
        getComponentAccessToken(componentVerifyTicket, componentAppsecret, componentAppid);
    }

}
